from starfield.Entity import Entity
from starfield import NameGenerator
import starfield
from mathutils import Vector
import random

class Ship(Entity):
	def __init__ (self, location = Vector([0.0, 0.0, 0.0])):
		if type(location) == list:
			location = Vector(location)
		
		
		
		Entity.__init__(self, location)
		self.namer = NameGenerator.NameGenerator(random.randint(0, 1000), 2)
		self.namer.inputFile("star_names.txt")
		self.name = self.namer.makeName()
		self.components = {}
		self.speed = random.uniform(3000, 6000) * starfield.SCALE
		self.target = "null"
		self.id = 0
		self.player = ""
		self.state = 0 #not moving
		self.star = 0 #id of system it is in
		
		#nice to know what I am
		self.role = "ship"
		self.revision = 0
		
		# other
		self.last_update = 0
		self.arrival_time = 0
		
		#client specific
		self.game_object = 0
		
		self.client_side = 0
		
	def serialize(self):
		return [self.id, self.name, self.player, list(self.location), self.star, self.speed]
		
	def unpack(self, data):
		self.id = data[0]
		self.name = data[1]
		self.player = data[2]
		self.location = Vector(data[3])
		self.star = data[4]
		self.speed = data[5]
		
		#we must be running on the client
		self.client_side = 1
	
	# time is delta time, since last update #
	def update(self, time):
		Entity.update(self, time)
		
		if self.game_object != 0:
			self.game_object.position = self.location
			self.game_object.alignAxisToVect(self.velocity, 1, .1)
		
	def stop(self):
		Entity.stop(self)
		
		#let the player know
		if self.client_side == 0:
			for entry in starfield.GameMaster.players:
				if entry.name == self.player:
					entry.explore(starfield.GameMaster.galaxy.stars_by_id[ self.star ])
		#else:
		#	starfield.client.players[self.player]
			
	def addComponent(self, component):
		if component.name not in self.components:
			self.components[component.name] = component
			self.mass += component.mass
			self.speed += component.speed
			return "true"
		else:
			return "false"

	def removeComponent(self, name):
		if name in self.components:
			self.mass -= self.components[name].mass
			self.speed -= self.speed
			self.components.pop(name)
			return "true"
		else:
			return "false"